package com.woniu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.entity.ServiceRecordEn;
import com.woniu.pojo.ServiceRecord;
import com.woniu.vo.SearchCriteria;

import com.woniu.vo.ServiceRecordVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.woniu.service.IServiceRecordService;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author rsw
 * @since 2022-10-18
 */
@Api(tags = "服务记录接口")
@RestController
@RequestMapping("/service-record")
    public class ServiceRecordController extends BaseController{

        @Autowired
        private IServiceRecordService serviceRecordService;

        @GetMapping
        public Result queryAll(){
            List<ServiceRecord> serviceRecordList = serviceRecordService.list();

            return toDataResult(serviceRecordList) ;

        }

        @PostMapping
        public Result add(@RequestBody ServiceRecord serviceRecord){

            return toResult(serviceRecordService.save(serviceRecord));

        }

        @DeleteMapping("/{id}")
        public Result deleteById(@PathVariable Integer id){

            return toResult(serviceRecordService.removeById(id));
        }

        @PutMapping
        public Result modifyById(@RequestBody com.woniu.pojo.ServiceRecord serviceRecord){

            return toResult(serviceRecordService.updateById(serviceRecord));
        }


        @PostMapping("/search/{page}/{size}")
        public Result search(@PathVariable int page, @PathVariable int size, @RequestBody SearchCriteria searchCriteria){
            IPage<com.woniu.pojo.ServiceRecord> p=new Page<com.woniu.pojo.ServiceRecord>(page,size);
            QueryWrapper<com.woniu.pojo.ServiceRecord> wrapper=new QueryWrapper<>();
            if(searchCriteria.getName()!=null)wrapper.like("name",searchCriteria.getName());
            if(searchCriteria.getStartTime()!=null)wrapper.gt("create_time",searchCriteria.getStartTime());
            if(searchCriteria.getEndTime()!=null)wrapper.lt("create_time",searchCriteria.getEndTime());
            if(searchCriteria.getOther()!=null&&searchCriteria.getOther().size()>0){
                searchCriteria.getOther().forEach((key,value)->{
                wrapper.eq(key,value);
                });
            }
            if(searchCriteria.getOtherLike()!=null&&searchCriteria.getOtherLike().size()>0){
                searchCriteria.getOtherLike().forEach((key,value)->{
                wrapper.like(key,value);
                });
            }

            p = serviceRecordService.page(p, wrapper);
            PageResult pageResult=new PageResult();
            pageResult.setData(p.getRecords());
            pageResult.setTotal(p.getTotal());

            return toDataResult(pageResult);
        }

        @ApiOperation("详情记录分页条件查询")
        @PostMapping("/searchRecord/{currentPage}/{pageSize}")
    public Result searchServiceRecord(@PathVariable String currentPage, @PathVariable String pageSize, @RequestBody ServiceRecordVo serviceRecordVo) throws JsonProcessingException {

            System.out.println(serviceRecordVo+"ddddddddddddddddddddddddddddddddddddddddddddddd");
            PageResult recordList = serviceRecordService.searchRecord(currentPage, pageSize, serviceRecordVo);


            return toDataResult(recordList);

        }




        }

